/*
 * Copyright 2004-2008 the original author or authors.
 * Licensed under 3G门户
 */
package com.ulwx.database.dbpool;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.ulwx.tool.ArrayUtils;
import com.ulwx.tool.ObjectUtils;
import com.ulwx.tool.Path;

/**
 * @author 江威
 * @email jiang-wei@3g.net.cn/weijiang8410@163.com
 * @since 1.0
 */
public class ReadConfig {

	private Logger log = Logger.getLogger(ReadConfig.class);
	private Map<String, Map<String, String>> properties = null;
	private Map<String,Map<String,Map<String,String>>> slaveProperites=null;
	private Map<String, String[]> trans = null;
	private static ReadConfig config = new ReadConfig();

	private ReadConfig() {
		parse();
	}

	public static ReadConfig getInstance() {
		return config;
	}


	public Map<String, Map<String, Map<String, String>>> getSlaveProperites() {
		return slaveProperites;
	}

	public void setSlaveProperites(
			Map<String, Map<String, Map<String, String>>> slaveProperites) {
		this.slaveProperites = slaveProperites;
	}

	public Map<String, Map<String, String>> getProperties() {
		return properties;
	}

	public Map<String, String[]> getTrans() {
		return trans;
	}

	/**
	 * 读配置文件
	 * 
	 * @return
	 */
	private void parse() {

		Map<String, Map<String, String>> maps = new ConcurrentHashMap<String, Map<String, String>>();
		Map<String,Map<String,Map<String,String>>> slaveMaps = new ConcurrentHashMap<String,Map<String,Map<String,String>>>();
		Map<String, String[]> disTransMaps = new ConcurrentHashMap<String, String[]>();
		

		try {

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			// File file = new File(Path.getClassPath() + "config.xml");
			// Document doc = db.parse(file);
			Document doc = db.parse(Path.getResource("/dbpool.xml"));
			NodeList configList = doc.getElementsByTagName("dbpool");
			for (int m = 0; configList != null && m < configList.getLength(); m++) {
				Element config_el = (Element) configList.item(m);
				String configName = config_el.getAttribute("name");
				String onlyForDis = config_el.getAttribute("onlyForDis");
				String type = config_el.getAttribute("type");
				NodeList nList = config_el.getChildNodes();
				Map<String, String> map = new HashMap<String, String>();
				maps.put(configName, map);
				map.put("onlyForDis", onlyForDis);
				map.put("type", type);
				for (int i = 0; nList != null && i < nList.getLength(); i++) {
					Node propertye_nn=nList.item(i);
					
					//Element propertye_el = (Element) nList.item(i);
					if (propertye_nn.getNodeName().equals("property")) {
						
						String name = ((Element)propertye_nn).getAttribute("name");

						String value = ((Element)propertye_nn).getTextContent();
						map.put(name, value);
					}

				}
				
				Map<String,Map<String,String>> slaveServers=new HashMap<String,Map<String,String>>();
				slaveMaps.put(configName, slaveServers);
				NodeList nSlaveList =config_el.getElementsByTagName("server");
				for(int f=0; nSlaveList!=null && f<nSlaveList.getLength(); f++){
					Element server_el=(Element) nSlaveList.item(f);
					String serverName=server_el.getAttribute("name");
					NodeList nSlavePList = server_el.getChildNodes();
					Map<String, String> mapp = new HashMap<String, String>();
					slaveServers.put(serverName, mapp);
					for (int i = 0; nSlavePList != null && i < nSlavePList.getLength(); i++) {

						
						if (nSlavePList.item(i).getNodeName().equals("property")) {
							Element pro_el = (Element) nSlavePList.item(i);
							String name = pro_el.getAttribute("name");

							String value = pro_el.getTextContent();
							mapp.put(name, value);
						}

					}
				}
			}
			NodeList disTransactions = doc
					.getElementsByTagName("dis-transaction");
			for (int m = 0; disTransactions != null
					&& m < disTransactions.getLength(); m++) {
				Element trans = (Element) disTransactions.item(m);
				String name = trans.getAttribute("name");
				String[] values = trans.getTextContent().split(",");
				values = ArrayUtils.trim(values);
				disTransMaps.put(name, values);
			}

		} catch (Exception e) {
			log.error("读取XML文档异常。。。。", e);
		}
		properties = maps;
		trans = disTransMaps;
		slaveProperites=slaveMaps;

	}

	/**
	 * 测试配置文件
	 */
	public void testReadCfg() {

	}

	public static void main(String[] args) {
		// ReadConfig.parse();
		//
		// System.out.println(CollectionUtils.toString(ReadConfig.properties));
		// System.out.println(CollectionUtils.toString(ReadConfig.trans));
		ReadConfig rf=new ReadConfig();
		System.out.println(ObjectUtils.toString(rf.properties));
		System.out.println(ObjectUtils.toString(rf.slaveProperites));
	}
}
